import Vue from 'vue'
import Vuex from 'vuex'
import { index,getInfo,getToken,newStaffAutoReport } from "../api/index.js"
Vue.use(Vuex)
//转成一维数组
const flatten= (arr)=>{
	return arr.reduce((result, item)=> {
		return result.concat(item,(Array.isArray(item.FChild) ? flatten(item.FChild) : []));
	}, []);
};
const store = new Vuex.Store({
    state: {
		// 用户登录数据
		token:"",        	//用户token
	},
    mutations: {
		// 获取token和用户id
		get_token(state,token){
			state.token = token
		},
		// 获取设备码
		get_status(state,status){
			state.status = status
		},
		// 获取个人信息数据
		get_user_info(state,userinfo){
			state.UserName = userinfo.UserName
			state.UserNumber = userinfo.UserNumber
			state.FSystemRole = userinfo.FSystemRole
			state.IsDeveloper = userinfo.IsDeveloper
		},
		setPermission(state,Permission){
			state.Permission=[]
			var p=flatten(Permission);
			state.Permission=p;
		},
		
	},
    actions: {
		setPermission({ commit },data){
			commit("setPermission",data)
		},
		login({ commit },data){
			return new Promise((resolve, reject)=>{
				index({username:data.username,password:data.password}).then(res=>{
					if(res.code==200){
						store.token=res.data.tokenHead+ res.data.token
						uni.setStorageSync("token",res.data.tokenHead+ res.data.token)
					}
					resolve(res)
				}).catch( err => {
					reject(err)
				})
			})
		},
		get_user({commit}){
			return new Promise((resolve, reject)=>{
				getInfo().then(res=>{
					resolve(res)
				}).catch(err=>{
					
					reject(err)
				})
			})
		},
	}
})
export default store